#ifndef XGDefineEventIncluded
#define XGDefineEventIncluded

#include "XueYue\Base.j"

library XGDefineEvent requires XGbase
	globals
		hashtable Xg_DefineEvent = InitHashtable()
	endglobals
	function Xg_RegDefineEvent takes trigger trg,string s returns boolean
		local integer i=LoadInteger(Xg_DefineEvent,StringHash(s),0) + 1
		call SaveInteger(Xg_DefineEvent,StringHash(s),0,i)
		call SaveTriggerHandle(Xg_DefineEvent,StringHash(s),i,trg)
		return true
	endfunction
	
	function Xg_RegDefineEvent_Int takes trigger trg,integer e,integer f returns boolean
		local integer i=R2I(LoadReal(Xg_DefineEvent,e,0)) + 1
		call SaveReal(Xg_DefineEvent,e,0,I2R(i))
		call SaveTriggerHandle(Xg_DefineEvent,e,f,trg)
		return true
	endfunction

	function Xg_ExecuteTrigger takes string s returns nothing
		local integer all=LoadInteger(Xg_DefineEvent,StringHash(s),0)
		local integer i=1
		local trigger trg
		loop
			exitwhen i>all
			set trg=LoadTriggerHandle(Xg_DefineEvent,StringHash(s),i)
			if trg!=null then
				call ConditionalTriggerExecute(trg)
			endif
			set i = i + 1
		endloop
	endfunction
	
	function Xg_ExecuteTrigger_Int takes integer e,integer f returns nothing
		local integer all = R2I(LoadReal(Xg_DefineEvent,e,0))
		local integer i = 1
		local trigger trg
		loop
			exitwhen i > all
			set trg = LoadTriggerHandle(Xg_DefineEvent,e,f)
			if trg != null then
				call ConditionalTriggerExecute(trg)
			endif
			set i = i + 1
		endloop
	endfunction
endlibrary

#endif
